class Solution:
  def verifyPostorder(self, postorder: list) -> bool:
    def dfs(i, j):
      nonlocal postorder
      if i >= j:
        return True
      root = postorder[j]
      p = i
      while postorder[p] < root:
        p = p + 1
      m = p
      while postorder[p] > root:
        p = p + 1
      return p == j and dfs(i, m-1) and dfs(m, j-1)
    return dfs(0, len(postorder)-1)


def stringToIntegerList(input):
  import json
  return json.loads(input)


def main():
  import sys
  import io

  def readlines():
    for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
      yield line.strip('\n')

  lines = readlines()
  while True:
    try:
      line = next(lines)
      postorder = stringToIntegerList(line)

      ret = Solution().verifyPostorder(postorder)

      out = (ret)
      print(out)
    except StopIteration:
      break


'''
STATUS: LEARNT
TRIES:  N/A
TIME:   36 MS
MEMORY: 13.8 MB
'''
if __name__ == '__main__':
  main()
